import requests
import json
import random
import re
import chardet
from agent_pool import user_agent_list


class TongHua(object):
    """
    爬取同花顺网站基金排行
    """

    def __init__(self, typ, total, page):
        self.start_url = "http://fund.ijijin.cn/data/Net/info/all_F009_desc_{}_{}_{}_jsonp_g.html".format(
            typ, total, page)
        self.headers = {
            "Referer": "http://fund.10jqka.com.cn/datacenter/sy/",
            "User-Agent": random.choice(user_agent_list)
        }

    # 获取响应
    def get_content(self):
        respons = requests.get(self.start_url, headers=self.headers)
        return respons.content.decode("unicode_escape")

    # 处理结果
    def handle_result(self, content):
        if content:
            result = re.match(r"g\((.*)\)", content, re.S).group(1)
            # print(chardet.detect(result))
            return result

    # 保存结果
    def save_json(self, result):
        if result:
            # print(result)
            result = json.loads(result, encoding="unicode_escape")
            with open("./tonghuajijin.json", "w") as f:
                json.dump(result, f, indent=4, ensure_ascii=False)

    # 运行爬虫
    def run_spider(self):
        content = self.get_content()
        result = self.handle_result(content)
        self.save_json(result)


def main():
    tonghua = TongHua("0_0_1", "20", "0_0_0")
    tonghua.run_spider()


if __name__ == '__main__':
    main()
